package com.bitBusy.manageyourriches.activity;

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.Toast;

import com.bitBusy.manageyourriches.R;
import com.bitBusy.manageyourriches.alarmSystem.AlarmManagement;
import com.bitBusy.manageyourriches.alarmSystem.AlarmManagerBroadcastReceiver;
import com.bitBusy.manageyourriches.database.contactClasses.PaymentReminder;
import com.bitBusy.manageyourriches.utilityClasses.navigation.ActionBarChoice;

public class AlarmActivity extends Activity {
	
	/**Prompt*/

	/**string*/
	public static final String ALARMTEXT = "com.bitBusy.manageMyRiches.ALARMTEXT";
	
	public static final String ALARMDETAILS = "com.bitBusy.manageMyRiches.ALARMDETAILS";
	
	public static final String ALARMSET = "Reminder has been set";
	
	private static final int RECURRENTVALUES = 0;
	
	 private AlarmManagerBroadcastReceiver alarm;

	 private PaymentReminder myPrevReminder;
	 
	//private DatabaseInteractor myDBInteractor;

			@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm);
      alarm = new AlarmManagerBroadcastReceiver();
        setDetails();
	//	myDBInteractor = new DatabaseInteractor(this);


		  	}

	 /**
		 * 
		 */
		private void setDetails() {
			Intent intent = getIntent();
			if (intent.getExtras() != null)
			{
				PaymentReminder reminder = (PaymentReminder)intent.getExtras().getParcelable(AlarmActivity.ALARMDETAILS);
				if (reminder != null)
				{
					myPrevReminder = reminder;
					EditText alarmText = (EditText) findViewById(R.id.activity_alarm_note);
					alarmText.setText(reminder.getNote());
					DatePicker datePicker = (DatePicker) findViewById(R.id.activity_alarm_scheduleDatePicker);
					datePicker.updateDate(reminder.getYear(), reminder.getMonth(), reminder.getDay());
					TimePicker timePicker = (TimePicker) findViewById(R.id.activity_alarm_scheduleTimePicker);
					timePicker.setCurrentHour(reminder.getHour());
					timePicker.setCurrentMinute(reminder.getMinute());
					
				}
			}
		}

	/**
     * This is the onClick called from the XML to set a new notification 
     */
    public void setReminder(View the_view){

    	  String alarmText =  getAlarmText();
	        Calendar calendar = getCalendarObject();
	        PaymentReminder reminder = 
	        		new PaymentReminder(calendar.get(Calendar.DAY_OF_MONTH), calendar.get(Calendar.MONTH), 
	        				calendar.get(Calendar.YEAR), calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), alarmText,
	        				RECURRENTVALUES, RECURRENTVALUES, null, (int) System.currentTimeMillis());
	  
    	AlarmManagement.setAlarm(reminder);
    	Toast.makeText(getApplicationContext(), ALARMSET , Toast.LENGTH_SHORT).show();
    	if (myPrevReminder != null)
    	{
			AlarmManagement.deleteAlarm(myPrevReminder);

    	}
    	
        refresh();
        
        }
    
  
	/**
	 * 
	 */
	private void refresh() {
		EditText alarmText = (EditText) findViewById(R.id.activity_alarm_note);
		alarmText.setText(null);
		Calendar calendar = Calendar.getInstance();
		DatePicker datePicker = (DatePicker) findViewById(R.id.activity_alarm_scheduleDatePicker);
		datePicker.updateDate(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
		TimePicker timePicker = (TimePicker) findViewById(R.id.activity_alarm_scheduleTimePicker);
		timePicker.setCurrentHour(calendar.get(Calendar.HOUR_OF_DAY));
		timePicker.setCurrentMinute(calendar.get(Calendar.MINUTE));
	
	}

	/**
	 * @return
	 */
	private String getAlarmText() {
		EditText alarmText = (EditText) findViewById(R.id.activity_alarm_note);
		return alarmText.getText().toString();
	}

	/**
	 * @return
	 */
	private Calendar getCalendarObject() {
		Calendar calendar = Calendar.getInstance();
		DatePicker datePicker = (DatePicker) findViewById(R.id.activity_alarm_scheduleDatePicker);
		TimePicker timePicker = (TimePicker) findViewById(R.id.activity_alarm_scheduleTimePicker);
		calendar.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
		calendar.set(Calendar.MONTH, datePicker.getMonth());
		calendar.set(Calendar.YEAR, datePicker.getYear());
		calendar.set(Calendar.HOUR_OF_DAY, timePicker.getCurrentHour());
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MINUTE, timePicker.getCurrentMinute());
		return calendar;
		}

	@Override
    protected void onStart() {
     super.onStart();
    }
          public void cancelRepeatingTimer(View view){
         Context context = this.getApplicationContext();
         if(alarm != null){
          alarm.CancelAlarm(context);
         }else{
        	 Toast.makeText(context, "Alarm is null", Toast.LENGTH_SHORT).show();
         }
        }
   
       
             

     
    @Override
    protected void onStop() {
          super.onStop();
    }


    public void editReminders(View the_view)
    {
    	Intent intent = new Intent(this, EditReminderActivity.class);
    	startActivity(intent);
    }
	
    
	 @Override
	  public boolean onCreateOptionsMenu(Menu the_menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.actionbar_menu, the_menu);
	    return true;
	  }
	 
	  
	  @Override
	  public boolean onOptionsItemSelected(MenuItem the_item) {
	    ActionBarChoice.choiceMade(this, the_item.getItemId());

	    return true;
	  }
}
